﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCDataCenter;

public partial class WebUserControl_KhaiBaoMatHang : System.Web.UI.UserControl
{
    static string error = "";
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static bool flag = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
                displayNganhHang();
                displayNhanHieu();
                displaySanPham();
                displayThue();
                displayTrangThai();

        }
    }
    void displaySanPham()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_FISRT_inprodct", CommandType.StoredProcedure);
        DropSanPham.DataSource = db;
        DropSanPham.DataTextField = "description";
        DropSanPham.DataValueField = "productkey";
        DropSanPham.DataBind();
    }
    void displayTrangThai()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_INABC", CommandType.StoredProcedure);
        DropTrangThai.DataSource = db;
        DropTrangThai.DataTextField = "description";
        DropTrangThai.DataValueField = "abckey";
        DropTrangThai.DataBind();
       
       
    }
    void displayNganhHang()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_FISRT_INCOMMOD", CommandType.StoredProcedure);
        DropNganhHang.DataSource = db;
        DropNganhHang.DataTextField = "description";
        DropNganhHang.DataValueField = "commodkey";
        DropNganhHang.DataBind();
    }
    void displayThue()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_ARTAX", CommandType.StoredProcedure);
        DropThueNhap.DataSource = db;
        DropThueNhap.DataTextField = "saletaxdesc";
        DropThueNhap.DataValueField = "saletaxkey";
        DropThueNhap.DataBind();
    }
    void displayNhanHieu()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_FISRT_INCLASS", CommandType.StoredProcedure);
        DropNhanHieu.DataSource = db;
        DropNhanHieu.DataTextField = "description";
        DropNhanHieu.DataValueField = "inclasskey";
        DropNhanHieu.DataBind();
    }  
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        BindGrid();
    }
    void BindGrid()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "SEARCH_INMAST", CommandType.StoredProcedure,
            new SqlParameter("@parameter", txtFind.Text));

        GridView1.DataSource = db;
        GridView1.DataBind();  
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
       
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        if (e.CommandName == "chon")
        {
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getInmast", CommandType.StoredProcedure,
                        new SqlParameter("@itemkey", grv.Cells[0].Text));
            if (db.Rows.Count > 0)
            {
                txtMaHang.Text = db.Rows[0]["itemkey"].ToString();
                txtMoTa.Text = db.Rows[0]["description"].ToString();
                txtMaHang2.Text = db.Rows[0]["itemkey1"].ToString();
                txtMoTa2.Text = db.Rows[0]["Description1"].ToString();
                txtBarcode.Text = db.Rows[0]["SMSCode"].ToString();
                txtDVT.Text = db.Rows[0]["ConVertDesc"].ToString(); 
                try
                {
                    DropThueNhap.SelectedValue = db.Rows[0]["saletaxkey"].ToString(); 
                }
                catch
                {
                }
                try
                {
                    DropNganhHang.SelectedValue = db.Rows[0]["commodkey"].ToString(); 
                }
                catch
                {
                }
                try
                {
                    DropSanPham.SelectedValue = db.Rows[0]["productkey"].ToString();
                }
                catch
                {
                }
                try
                {
                    DropNhanHieu.SelectedValue = db.Rows[0]["inclasskey"].ToString();
                }
                catch
                {
                }
                txtGiaBan.Text = db.Rows[0]["price"].ToString();
                try
                {
                    DropTrangThai.SelectedValue = db.Rows[0]["abckey"].ToString();
                }
                catch
                {
                }
                CheckQuet.Checked = bool.Parse(db.Rows[0]["mustBeScanned"].ToString());
                TabContainer1.ActiveTabIndex = 0;
                flag = false;
                txtMaHang.ReadOnly = true;
            }

        }
    }
    protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
    {
        Clear();
        flag = true;
    }
    protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void ImageButton7_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        string kq = "";
        try
        {
            if (flag)
            {
                kq = dt.GetDataDepend(str, "ls_saveInmastInfo", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@itemKey", txtMaHang.Text),
                    new SqlParameter("@itemDesc", txtMoTa.Text),
                    new SqlParameter("@itemKey2", txtMaHang2.Text),
                    new SqlParameter("@itemDesc2", txtMoTa2.Text),
                    new SqlParameter("@barCode", txtBarcode.Text),
                    new SqlParameter("@cvrtDesc", txtDVT.Text),
                    new SqlParameter("@VATRate", DropThueNhap.SelectedValue.ToString()),
                    new SqlParameter("@Category", DropNganhHang.SelectedValue.ToString()),
                    new SqlParameter("@Brand", DropNhanHieu.SelectedValue.ToString()),
                    new SqlParameter("@Product", DropSanPham.SelectedValue.ToString()),
                    new SqlParameter("@price", float.Parse(txtGiaBan.Text)),
                    new SqlParameter("@status", DropTrangThai.SelectedValue.ToString()),
                    new SqlParameter("@flgNew", 2),
                    new SqlParameter("@mustBeScanned",CheckQuet.Checked));
                if (kq != "0")
                {
                    uscMsgBox1.AddMessage(kq, uscMsgBox.enmMessageType.Error);
                }
                else
                {
                    uscMsgBox1.AddMessage("Hoàn thành lưu thông tin", uscMsgBox.enmMessageType.Error);
                    flag = false;
                    txtMaHang.ReadOnly = true;
                }
            }
            else
            {
                kq = dt.GetDataDepend(str, "ls_saveInmastInfo", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@itemKey", txtMaHang.Text),
                    new SqlParameter("@itemDesc", txtMoTa.Text),
                    new SqlParameter("@itemKey2", txtMaHang2.Text),
                    new SqlParameter("@itemDesc2", txtMoTa2.Text),
                    new SqlParameter("@barCode", txtBarcode.Text),
                    new SqlParameter("@cvrtDesc", txtDVT.Text),
                    new SqlParameter("@VATRate", DropThueNhap.SelectedValue.ToString()),
                    new SqlParameter("@Category", DropNganhHang.SelectedValue.ToString()),
                    new SqlParameter("@Brand", DropNhanHieu.SelectedValue.ToString()),
                    new SqlParameter("@Product", DropSanPham.SelectedValue.ToString()),
                    new SqlParameter("@price", float.Parse(txtGiaBan.Text)),
                    new SqlParameter("@status", DropTrangThai.SelectedValue.ToString()),
                    new SqlParameter("@mustBeScanned", CheckQuet.Checked));
                if (kq != "0")
                {
                    uscMsgBox1.AddMessage(kq, uscMsgBox.enmMessageType.Error);
                }
                else
                {
                    uscMsgBox1.AddMessage("Hoàn thành lưu thông tin", uscMsgBox.enmMessageType.Error);
                }
               
            }
           
        }
        catch
        {
        }
    }
    protected void txtFind_TextChanged(object sender, EventArgs e)
    {
        BindGrid();
    }
    void Clear()
    {
        txtMaHang.ReadOnly = false;
        txtMaHang.Text = "";
        txtMoTa.Text = "";
        txtMaHang2.Text = "";
        txtMoTa2.Text = "";
        txtGiaBan.Text = "";
        txtBarcode.Text = "";
        txtDVT.Text = "";
    }
}
